package com.dao;

import com.entity.admin;
import com.entity.user;

import java.util.List;

public class logindaoimpl implements logindao {
    @Override
    public boolean Login(String id, String pwd)
    {
        boolean flag=false;
        String sql="select * from user";
        UserInfDao u=new UserInfDao();
        List<user> list=u.getForList(user.class,sql);
        for (int i=0;i<list.size();i++)
        {
            if (list.get(i).getUserID().equals(id) && list.get(i).getUserPwd().equals(pwd)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public boolean register(String id,String pwd,String xm,String yx,String xb) {
        boolean flag1=true;
        String sql="select * from user";
        UserInfDao u=new UserInfDao();
        List<user> list= u.getForList(user.class,sql);
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getUserID().equals(id))
            {
                flag1=false;break;
            }
        }
        if(flag1)
        {
            String sql1="insert into user(UserID,UserPwd,Money,UserName,Email,UserSex) values(?,?,0,?,?,?)";
            u.update(sql1,id,pwd,xm,yx,xb);
        }
        return flag1;
    }

    @Override
    public boolean gLogin(String id, String pwd) {
        boolean flag=false;
        String sql="select * from admin";
        UserInfDao u=new UserInfDao();
        List<admin> list=u.getForList(admin.class,sql);
        for (int i=0;i<list.size();i++)
        {
            if (list.get(i).getUserID().equals(id) && list.get(i).getUserPwd().equals(pwd)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public boolean gregister(String id, String pwd) {
        boolean flag1=true;
        String sql="select * from admin";
        UserInfDao u=new UserInfDao();
        List<admin> list= u.getForList(admin.class,sql);
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getUserID().equals(id))
            {
                flag1=false;break;
            }
        }
        if(flag1)
        {
            String sql1="insert into admin(AdminID,AdminPwd) values(?,?)";
            u.update(sql1,id,pwd);
        }
        return flag1;
    }
}
